package com.hsxxherp.ation.oauth2.center.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hsxxherp.ation.oauth2.center.server.entity.UaaClient;
import com.hsxxherp.ation.oauth2.center.server.service.ISysTokenService;
import com.hsxxherp.ation.oauth2.center.server.service.IUaaClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController("/client")
public class ClientController {

    @Autowired
    IUaaClientService uaaClientService;

    @Autowired
    ISysTokenService sysTokenService;

    /**
     * 获取查询参数
     *
     * @param params
     * @return
     */
    public static LambdaQueryWrapper<UaaClient> getUaaClientLambdaQueryWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<UaaClient> queryWrapper = new LambdaQueryWrapper<>();
        if (params.get("client_id") != null) {
            queryWrapper.eq(UaaClient::getClientId, params.get("client_id"));
        }
        if (params.get("client_name") != null) {
            queryWrapper.eq(UaaClient::getClientName, params.get("client_name"));
        }
        return queryWrapper;
    }

    /**
     * 添加客户端信息
     *
     * @param params
     * @return
     */
    @PostMapping("/client")
    public String add(@RequestBody Map<String, Object> params) {
        UaaClient client = new UaaClient();
        client.setClientId((String) params.get("client_id"));
        client.setClientName((String) params.get("client_name"));
        client.setClientSecret("123456");
        client.setState(UaaClient.State.ENABLED);

        // 这里要验证是否有相同的id
        log.info(client.toString());
        uaaClientService.save(client);
        return "success";
    }

    /**
     * 按照pk修改（风险）
     *
     * @param params
     * @param clientId
     * @return
     * @throws Exception
     */
    @PutMapping("/client/{clientID}")
    public boolean update(@RequestBody Map<String, Object> params, @PathVariable("clientID") String clientId) throws Exception {
        UaaClient client = getUaaClientEntity(params);
        LambdaUpdateWrapper<UaaClient> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UaaClient::getClientId, clientId);
        return uaaClientService.saveOrUpdate(client, updateWrapper);
    }

    /**
     * 获取客户端详情
     *
     * @param clientID
     * @return
     */
    @RequestMapping(value = "/client/{clientID}", method = RequestMethod.GET)
    public UaaClient getUaaClient(@PathVariable String clientID) {
        LambdaQueryWrapper<UaaClient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UaaClient::getClientId, clientID);
        return uaaClientService.getOne(queryWrapper);
    }

    /**
     * 获取用户列表,全参数查询
     *
     * @param params
     * @return
     */
    @PostMapping("/client/list")
    public List<UaaClient> getUaaClients(@RequestBody Map<String, Object> params) {
        LambdaQueryWrapper<UaaClient> queryWrapper = getUaaClientLambdaQueryWrapper(params);
        return uaaClientService.list(queryWrapper);
    }

    private UaaClient getUaaClientEntity(Map<String, Object> params) throws Exception {
        UaaClient client = new UaaClient();
        if (params.get("client_id") != null) {
            client.setClientId(params.get("client_id").toString());

        }
        if (params.get("client_name") != null) {
            client.setClientName(params.get("client_name").toString());
        } else {
            throw new Exception("参数【client_name】不可为空！");
        }
        return client;
    }

    /**
     * 参数类型枚举
     */
    static enum ParamType {
        SAVEN,
        UPDATE,
        SELECT
    }
}
